import math

import pyautogui
import uiautomation as auto
# import AutoItLibrary as lib
import os
import sys
import time
import subprocess
import OCR as ocr

#关闭UClient The process name to be brought to an abrupt halt
process_name = "UClient.exe"

# Employing the taskkill command to terminate the process
os.system(f"taskkill /f /im {process_name}")

#点击元素
def elementClick(element:str):
    pos = ocr.getCoordinate(element)
    X = int(math.floor(pos[0]))
    Y = int(math.floor(pos[1]))
    auto.Click(X, Y, waitTime=1)

def elementDClick(element:str):
    pos = ocr.getCoordinate(element)
    X = int(math.floor(pos[0]))
    Y = int(math.floor(pos[1]))
    auto.Click(X, Y, waitTime=1)
    auto.Click(X, Y, waitTime=1)

#打开软件
subprocess.Popen('C:\\Users\\admin\Desktop\工作资料\\UClient.exe')

#窗口捕获
window = auto.WindowControl(searchDepth=1, ClassName='YonyouUWnd')

#线程强制停止，防止截图识别功能同步进行
time.sleep(3)

#1、单击应用
# 左键单击事件auto.Click(x: int, y: int, waitTime: float = OPERATION_WAIT_TIME)
pos = ocr.getCoordinate("172.88.1.61:89")
X = int(math.floor(pos[0]))
Y = int(math.floor(pos[1]))
# auto.Click(1726, 950, waitTime = 1)
auto.Click(X, Y, waitTime = 1)
# auto.Click(150, 13, waitTime = 1)

# [[[55.0, 458.0], [196.0, 463.0], [195.0, 491.0], [54.0, 486.0]], ('172.88.1.61:89', 0.9989128708839417)]
# [[55.0, 460.0], [196.0, 465.0]]


#线程强制停止，防止截图识别功能同步进行
time.sleep(30)


#2、输入密码
#获取密码坐标
password = ocr.getCoordinate("密码")
password_clone = ocr.getCoordinate("登录")
# password_clone = ocr.getCoordinate("选项《")
# moveTo(password_clone[0], password[1])
X = int(math.floor(password_clone[0]))
Y = int(math.floor(password[1]))
auto.Click(X, Y, waitTime = 1)

#线程强制停止，防止截图识别功能同步进行
time.sleep(2)

#操作键盘
auto.SendKeys('{h 1}{q 1}{2 1}{0 1}{2 1}{4 1}')

#线程强制停止，防止截图识别功能同步进行
time.sleep(2)

#点击登录按钮
Y = int(math.floor(password_clone[1]))
auto.Click(X, Y, waitTime = 1)

#线程强制停止，防止截图识别功能同步进行
time.sleep(2)

#3、选择账套信息
try:
    #获取账套坐标
    pos = ocr.getCoordinate("集团测试1127")
    X = int(math.floor(pos[0]))
    Y = int(math.floor(pos[1]))
    auto.Click(X, Y, waitTime = 1)

    #线程强制停止，防止截图识别功能同步进行
    time.sleep(1)

    pos = ocr.getCoordinate("集团测试0223")
    X = int(math.floor(pos[0]))
    Y = int(math.floor(pos[1]))
    auto.Click(X, Y, waitTime = 1)
except Exception as e:
    pass

#线程强制停止，防止截图识别功能同步进行
time.sleep(1)

#点击确定  可以转换成enter键值输入
pos = ocr.getCoordinate("确定")
X = int(math.floor(pos[0]))
Y = int(math.floor(pos[1]))
auto.Click(X, Y, waitTime = 1)

#线程强制停止，防止截图识别功能同步进行
time.sleep(5)

#3、进入主界面开始执行相关业务
#点击财务会计
res = elementClick("财务会计")
#点击总账
res = elementClick("总账")
#点击凭证管理
pos = ocr.getCoordinate("凭证管理")
X = int(math.floor(pos[0]))
Y = int(math.floor(pos[1]))
auto.Click(X-20, Y, waitTime = 1)
#点击审核
pos = ocr.getCoordinate("审核")
X = int(math.floor(pos[0]))
Y = int(math.floor(pos[1]))
pyautogui.click(X, Y, clicks=2, interval=0.1, duration=3.0)
#线程强制停止，防止截图识别功能同步进行
time.sleep(5)
#点击查询
res = elementClick("查询卡片显示")
#线程强制停止，防止截图识别功能同步进行
time.sleep(5)
#选择账套
pos = ocr.getCoordinate("制单系统")
X = int(math.floor(pos[0] + 10))
Y = int(math.floor(pos[1] + 5) )
auto.Click(X, Y, waitTime = 1)
#线程强制停止，防止截图识别功能同步进行
time.sleep(5)
#选择账簿--华强集团2006
res = elementClick("华强集团2006")
#直接滑轮向下一次
auto.WheelDown(wheelTimes=1, interval=0.05, waitTime=1)
#选择账簿--华强集团2006
res = elementClick("华强集团2006")




















# 显示搜索控件所遍历的控件数和搜索时间
# auto.uiautomation.DEBUG_SEARCH_TIME = True  # 设置全局搜索超时时间为1秒
# auto.uiautomation.SetGlobalSearchTimeout(1)  # 创建计算器窗口控件
# calcWindow = auto.WindowControl(searchDepth=1, name="Yongyou UClient")
# print(calcWindow.Exists(0, 0))
# subprocess.Popen('C:\\Users\\admin\Desktop\工作资料\\UClient.exe')
# calcWindow = auto.WindowControl(
#     searchDepth=1, ClassName='Yongyou UClient',
#     Compare=lambda c, d: c.Name == 'Yongyou UClient' or c.Name == 'Yongyou UClient', desc='Yongyou UClient')
# if not calcWindow.Exists(0, 0):
#     subprocess.Popen('Yongyou UClient')  # 设置窗口前置
#     calcWindow.SetTopmost(True)


# c = auto.GetRootControl()
# print(c)


# def GetFirstChild(control):
#     return control.GetFirstChildControl()
# def GetNextSibling(control):
#     return control.GetNextSiblingControl()
#
# desktop = auto.GetRootControl()
# for control, depth in auto.WalkTree(desktop, getFirstChild=GetFirstChild, getNextSibling=GetNextSibling,
#                                     includeTop=True, maxDepth=2):
#     if not control.Name:
#         continue
#     print(' ' * depth * 4, control.Name)

